<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreWeightConvert {
	var $classes;
	var $rules;
	
	//constructor
	function CoreWeightConvert() {
		$this->init();
	}
	
	//
	function init() {
		$this->classes = array(
					'g' => 'Gram(s)',
					'kg' => 'Kilogram(s)',
					'oz' => 'Ounce(s)',
					'lb' => 'Pound(s)',
						);
		
		$this->rules = array(
					//g
					'g:kg' => '0.0010',
					'g:oz' => '0.0352',
					'g:lb' => '0.0022',
					//kg
					'kg:g' => '1000.0000',
					'kg:oz' => '35.2739',
					'kg:lb' => '2.2046',
					//oz
					'oz:g' => '28.3495',
					'oz:kg' => '0.0283',
					'oz:lb' => '0.0625',
					//lb
					'lb:g' => '453.5923',
					'lb:kg' => '0.4535',
					'lb:oz' => '16.0000',
						);
	}
	
	//methods
	function convert($value, $from = 'kg', $to = 'lb') {
		if( in_array($from, array_keys($this->classes)) && in_array($to, array_keys($this->classes)) ) {
			$rule = $this->rules[$from . ':' . $to];
			
			return number_format($value * $rule, 4);
		} else {
			return $value;
		}
	}
}

//
class CoreWeight extends CoreWeightConvert {
	
	//constructor
	function CoreWeight() {
		parent::CoreWeightConvert();
	}
	
	//methods
}
//
?>